package com.zq.vaccinum.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zq.vaccinum.constant.Constants;
import com.zq.vaccinum.domain.JsonResult;
import com.zq.vaccinum.util.WebUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 认证失败异常处理
 * @Author: zq
 * @Date: 2023/08/08/13:35
 * @Description:
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        JsonResult jsonResult = new JsonResult(Constants.UNAUTHORIZED,"用户认证未通过请重新登录");
        String json = new ObjectMapper().writeValueAsString(jsonResult);
        WebUtils.renderString(response,json);
    }
}
